﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;
using InfoPick.Lib;
using Telerik.Windows.Controls;
using InfoPick.Lib.Track;

namespace InfoPick.UI.Views
{
    /// <summary>
    /// Interaction logic for TrackInfo.xaml
    /// </summary>
    public partial class TrackInfo : Page
    {
        List<MSCInfo> msciList = null;
        List<MSCInfo> msciTxtList = null;
        List<EGInfo> egList = null;
        List<EGInfo> resultList = null;
        public TrackInfo()
        {
            InitializeComponent();
        }
        /// <summary>
        /// msci文件打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMscOpen_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog fileDlg = new System.Windows.Forms.OpenFileDialog();

            fileDlg.Title = "Msc";
            fileDlg.Filter = @"Excel文件(*.xlsx)|*.xlsx|Excel 97-2003文件(*.xls)|*.xls";

            fileDlg.Multiselect = false;
            
            if (fileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (msciTxtList != null)
                {
                    msciTxtList.Clear();
                }
                string FileName = fileDlg.FileName;
                mscText.Text = FileName;       
            }
        }
        /// <summary>
        /// eg文件打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEGOpen_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog fileDlg = new System.Windows.Forms.OpenFileDialog();

            fileDlg.Title = "CQG";
            fileDlg.Filter = @"txt文件(*.txt)|*.txt";

            fileDlg.Multiselect = false;
            if (fileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (egList != null)
                {
                    egList.Clear();
                }
                string FileName = fileDlg.FileName;
                egText.Text = FileName;
            }
        }
        /// <summary>
        /// 分析按键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAnalysis(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(mscText.Text))
            {
                System.Windows.Forms.MessageBox.Show("请打开Msc号码对应文件！", "错误");
                return;
            }
            if (string.IsNullOrEmpty(egText.Text))
            {
                System.Windows.Forms.MessageBox.Show("请打开E/G文件！", "错误");
                return;
            }
            if (string.IsNullOrEmpty(Time_textBox.Text))
            {
                System.Windows.Forms.MessageBox.Show("请填入REQ搜索时间！", "错误");
                return;
            }
            if (string.IsNullOrEmpty(Time2_textBox.Text))
            {
                System.Windows.Forms.MessageBox.Show("请填入CNF搜索时间！", "错误");
                return;
            }
            if (!File.Exists(mscText.Text))
            {
                System.Windows.Forms.MessageBox.Show(mscText.Text + " 文件不存在！", "错误");
                return;
            }
            string timeStr = Time_textBox.Text;
            int time1 = 0;
            if (int.TryParse(timeStr, out time1) == false)
            {
                System.Windows.Forms.MessageBox.Show("REQ搜索时间格式错误！", "错误");
                return;
            }
            else
            {
                time1 = Convert.ToInt32(timeStr);
            }
            string time2Str = Time2_textBox.Text;
            int time2 = 0;
            if (int.TryParse(time2Str, out time2) == false)
            {
                System.Windows.Forms.MessageBox.Show("REQ搜索时间格式错误！", "错误");
                return;
            }
            else
            {
                time2 = Convert.ToInt32(time2Str);
            }
            ////清除上一次的mscilist
            if (msciList != null)
            {
                msciList.Clear();
            }
            msciList = MSCInfo.Load(mscText.Text);
            ////判断eg文件是否存在
            if (!File.Exists(egText.Text))
            {
                System.Windows.Forms.MessageBox.Show(egText.Text + " 文件不存在！", "错误");
                return;
            }
            ////清除上一次的eglist
            if (egList != null)
            {
                egList.Clear();
            }
            ////读取eg文件到list
            egList = EGInfo.Load(egText.Text);

            if (msciTxtList != null)
            {
                msciTxtList.Clear();
            }
            ///////判断lai文件名存在
            if (!string.IsNullOrEmpty(laiText.Text.ToString()))
            {
                if (!File.Exists(laiText.Text))
                {
                    System.Windows.Forms.MessageBox.Show(laiText.Text + " 文件不存在！", "错误");
                    return;
                }
                msciTxtList = MSCInfo.Loadtxt(laiText.Text);
                if (msciTxtList == null)
                {
                    System.Windows.Forms.MessageBox.Show("lai文件数据错误！", "错误");
                    return;
                }
            }
            
            /////判断eg文件打开后的数据
            if (egList == null)
            {
                System.Windows.Forms.MessageBox.Show("E/G文件数据错误！", "错误");
                return;
            }
            else
            {
                if (egList.Count == 0)
                {
                    System.Windows.Forms.MessageBox.Show(egText.Text + "没有数据或者格式不正确！", "信息");
                    return;
                }
            }
            ////判断msci打开后的数据
            if (msciList == null)
            {
                System.Windows.Forms.MessageBox.Show("Msc号码对应文件错误！", "错误");
                return;
            }
            else
            {
                if (msciList.Count == 0)
                {
                    System.Windows.Forms.MessageBox.Show(mscText.Text + "没有数据或者格式不正确！", "信息");
                    return;
                }
            }
            /////清除上次查询记录
            if (resultList != null)
            {
                if (resultList.Count > 0)
                    resultList.Clear();
            }
            
            string fileName = egText.Text;
            /////分析数据
            resultList = EGAnalysis.Analysis(egList, msciList,msciTxtList, time1, time2, fileName);
            ///绑定到view
            RadGridView_EGList.ItemsSource = resultList.Where(c => !c.invalid_cell).ToList();
            RadGridView_EGList_2.ItemsSource = resultList.Where(c => c.invalid_cell).ToList();
            ////判断输出结果为0条记录提示
            if (resultList != null)
            {
                if (resultList.Count == 0)
                {
                    System.Windows.Forms.MessageBox.Show("分析结果: 0条记录！", "信息");
                }
            }
        }
        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            if (resultList == null)
            {
                System.Windows.Forms.MessageBox.Show("没有数据，不能导出！", "错误");
                return;
            }
            else if (resultList.Count == 0)
            {
                System.Windows.Forms.MessageBox.Show("没有数据，不能导出！", "错误");
                return;
            }
            List<EGInfo> list1 = RadGridView_EGList.ItemsSource as List<EGInfo>;
            List<EGInfo> list2 = RadGridView_EGList_2.ItemsSource as List<EGInfo>;

            
            SaveFileDialog dialog = new SaveFileDialog();
            string extensioin = "xls";
            string SelectedExportFormat = "excel";
            dialog.DefaultExt = extensioin;
            dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extensioin, SelectedExportFormat);

            dialog.FilterIndex = 1;

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {           
                        string sheet1 = tab1.Text;
                        string sheet2 = tab2.Text;
                        ExportExcel.Export(dialog.FileName, sheet1, list1, sheet2, list2);
                        System.Windows.Forms.MessageBox.Show("导出成功！", "信息");
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }

            }
        }
        /// <summary>
        /// 导出格式设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void view_ElementExporting(object sender, GridViewElementExportingEventArgs e)
        {
            if (e.Element == ExportElement.HeaderRow || e.Element == ExportElement.HeaderCell)
            {
                e.Background = Colors.LightCyan;
                e.FontSize = 12;
                e.FontWeight = FontWeights.Bold;
                e.Height = 50;
                e.Width = 100;
            }
        }
        /// <summary>
        /// 打开Lai文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLaiOpen_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog fileDlg = new System.Windows.Forms.OpenFileDialog();

            fileDlg.Title = "CQG";
            fileDlg.Filter = @"txt文件(*.txt)|*.txt";

            fileDlg.Multiselect = false;
            if (fileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (msciTxtList != null)
                {
                    msciTxtList.Clear();
                }
                string FileName = fileDlg.FileName;
                laiText.Text = FileName;
            }
        }

        private void Tab1_SelectionChanged(object sender, RadSelectionChangedEventArgs e)
        {

        }

    }
}
